<?php

namespace App\Http\Controllers\Admin;

use App\Http\Filters\EventFilter;
use App\Models\Event;
use App\Http\Requests\EventRequest;
use App\Http\Resources\EventResource;
use App\Models\Housings;
use App\Models\Project;
use Illuminate\Http\Request;

class EventController extends Controller
{
    public function index(EventFilter $filter)
    {
        $events = Event::query()
//            ->with('housings')
            ->filter($filter)
            ->paginate();

        return $this->ok(EventResource::collection($events));
    }

    public function create()
    {
        return $this->ok();
    }

    public function store(EventRequest $request)
    {
        $inputs = $request->validated();
        $event = Event::create($inputs);

        return $this->created(EventResource::make($event));
    }

    public function edit(Request $request, Event $event,$id)
    {
        $report = Event::findOrFail($id);
        return $this->ok(EventResource::make($report)->for(EventResource::FOR_EDIT)->additional($this->formData($report)));
    }
    protected function formData($report)
    {
        $housings = Housings::query()
            ->where('id',$report['housings_id'])
            ->select(['id', 'hou_address','housings_num'])
            ->orderByDesc('id')
            ->first();
        return compact('housings');
    }

    public function update(EventRequest $request, Event $event,$id)
    {
        $inputs = $request->validated();
        $report = Event::findOrFail($id);
        $report->update($inputs);
        return $this->created(EventResource::make($report));
    }

    public function destroy(Event $event)
    {
        $event->delete();
        return $this->noContent();
    }
}
